﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace 护士工作站
{
    public partial class 新增医嘱处理 : Form
    {
        public 新增医嘱处理()
        {
            InitializeComponent();
            f1 = this;
        }
        public DataTable dt = new DataTable();
        public DataTable dts = new DataTable();

        public static 新增医嘱处理 f1 = null;

        private void button_qx_Click(object sender, EventArgs e)
        {
            this.Hide();
           
        }

        public void button_czyz_Click(object sender, EventArgs e)
        {
            string sql = "select max(WarnNo) from Warn where HosNo='" + this.tb_ZYNo.Text + "'";
            string dw = "select MedDanwei from MedicinePrice where MedName='" + this.tb_nr.Text + "'";
            string dj = "select Medprice from MedicinePrice where MedName='" + this.tb_nr.Text + "'";
            string gg = "select MedModel from MedicinePrice where MedName='" + this.tb_nr.Text + "'";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                SqlCommand c = new SqlCommand(sql, conn);
                conn.Open();
                int n = Convert.ToInt32(c.ExecuteScalar()); 
                DataRow dr = null;
                dr = dt.NewRow();
                dr[0] = this.tb_ZYNo.Text;
                dr[1] = n + 1;
                dr[2] = this.cb_cl.Text;
                dr[3] = this.cb_lb.Text;
                dr[4] = this.dtp_kssj.Value;
                dr[5] = this.tb_nr.Text;
                dr[6] = this.tb_ypjl.Text;
                dr[7] = this.cb_tj.Text;
                dr[8] = this.tb_cs.Text;
                dr[9] = this.tb_zxsj.Text;
                dr[10] = this.dtp_tzsj.Value;
                dr[11] = "执行";
                dr[12] = this.cb_sl.Text;
                dt.Rows.Add(dr);
             this.dataGridView1.DataSource = dt;
                SqlCommand comMedDanwei = new SqlCommand(dw, conn);
                SqlCommand comMedprice = new SqlCommand(dj, conn);
                SqlCommand comMedModel = new SqlCommand(gg , conn);
                string Danwei = Convert.ToString(comMedDanwei.ExecuteScalar());
                double   Medprice = Convert .ToDouble ( comMedprice.ExecuteScalar());
                string Model = Convert.ToString(comMedModel.ExecuteScalar());
                int x = Convert.ToInt32(cb_sl.Text);

               float   m =  x *(float ) Medprice;
                DataRow drs = null;
                drs = dts.NewRow();
                drs[0] = this.tb_ZYNo.Text;
                drs[1] = n + 1;
                drs[2] = this.tb_nr.Text;
                drs[3] = Danwei;
                drs[4] = Medprice;
                drs[5] = Model;
                drs[6] = m;
                drs[7] = x;
                drs[8] = m;
                

                string HosBalance = "select HosBalance from InHospital where HosNo='" + tb_ZYNo.Text.Trim () + "'";
                SqlCommand com = new SqlCommand(HosBalance, conn);
                double Balance = Convert.ToDouble(com.ExecuteScalar());

                string HosTotal = "select HosTotal from InHospital where HosNo='" + tb_ZYNo.Text.Trim() + "'";
                SqlCommand cm = new SqlCommand(HosTotal, conn);
                double Total = Convert.ToDouble(cm.ExecuteScalar());
                float NewTotal = ((float)Total + m);
                string upt = string.Format("update InHospital set HosBalance='" + NewTotal + "'where InHospital.HosNo='" + tb_ZYNo.Text.Trim() + "'");
                SqlCommand cop = new SqlCommand(upt , conn);
                int j = Convert.ToInt32(cop.ExecuteNonQuery());

                    float  NewBalance = ((float)Balance - m); 
                    if (Balance > 0)
                    {
                        string abc = string.Format("update InHospital set HosBalance='" + NewBalance + "'where InHospital.HosNo='" + tb_ZYNo.Text.Trim() + "'");
                        SqlCommand cam = new SqlCommand(abc, conn);
                        int i = Convert.ToInt32(cam.ExecuteNonQuery());

                        drs[9] = m;
                        drs[10] = "已付款";
                        dts.Rows.Add(drs);
                        if (i == 1&&j==1)
                        {
                            MessageBox.Show("医嘱成组成功！");

                        }
                    }
                    else 
                    { 
                        drs[9] = 0;
                        drs[10] = "未付款";
                        dts.Rows.Add(drs);
                        if (j == 1)
                        MessageBox.Show("医嘱成组成功！");
                    }
            }
        }

        public void 新增医嘱处理_Load(object sender, EventArgs e)
        {
            DataColumn dc_zyh = new DataColumn("住院号", typeof(string));
            DataColumn dc_bh = new DataColumn("编号", typeof(int));
            DataColumn dc_cl = new DataColumn("长/临", typeof(string));
            DataColumn dc_lb = new DataColumn("类别", typeof(string));
            DataColumn dc_kssj = new DataColumn("开始时间");
            DataColumn dc_yznr = new DataColumn("医嘱内容", typeof(string));
            DataColumn dc_jl = new DataColumn("剂量", typeof(string));
            DataColumn dc_tj = new DataColumn("途径", typeof(string));
            DataColumn dc_cs = new DataColumn("次数", typeof(string));
            DataColumn dc_zxsj = new DataColumn("执行时间", typeof(string));
            DataColumn dc_tzsj = new DataColumn("停止时间");
            DataColumn dc_zt = new DataColumn("医嘱状态", typeof(string));
            DataColumn dc_ypsl = new DataColumn("药品数量", typeof(string));

            DataColumn dc_Zyh = new DataColumn("病人住院号", typeof(string));
            DataColumn dc_Bh = new DataColumn("医嘱编号", typeof(int));
            DataColumn dc_nr = new DataColumn("内容", typeof(string));
            DataColumn dc_dw = new DataColumn("单位", typeof(string));
            DataColumn dc_gg = new DataColumn("药品规格");
            DataColumn dc_dj= new DataColumn("单价", typeof(string));
            DataColumn dc_je = new DataColumn("金额", typeof(string));
            DataColumn dc_sl = new DataColumn("数量", typeof(string));
            DataColumn dc_yfje = new DataColumn("应付金额", typeof(string));
            DataColumn dc_yije = new DataColumn("已付金额", typeof(string));
            DataColumn dc_fkzt = new DataColumn("付款状态", typeof(string));
            dt.Columns.Add(dc_zyh);
            dt.Columns.Add(dc_bh);
            dt.Columns.Add(dc_cl);
            dt.Columns.Add(dc_lb);
            dt.Columns.Add(dc_kssj);
            dt.Columns.Add(dc_yznr);
            dt.Columns.Add(dc_jl);
            dt.Columns.Add(dc_tj);
            dt.Columns.Add(dc_cs);
            dt.Columns.Add(dc_zxsj);
            dt.Columns.Add(dc_tzsj);
            dt.Columns.Add(dc_zt);
            dt.Columns.Add(dc_ypsl);

            dts.Columns.Add(dc_Zyh);
            dts.Columns.Add(dc_Bh);
            dts.Columns.Add(dc_nr);
            dts.Columns.Add(dc_dw);
            dts.Columns.Add(dc_dj);
            dts.Columns.Add(dc_gg);
            dts.Columns.Add(dc_je);
            dts.Columns.Add(dc_sl);
            dts.Columns.Add(dc_yfje);
            dts.Columns.Add(dc_yije);
            dts.Columns.Add(dc_fkzt);
        }



        private void button_bc_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                conn.Open();
                using (SqlBulkCopy sqlbulk = new SqlBulkCopy(conn))
                {
                    try
                    {



                        sqlbulk.BatchSize = dt.Rows.Count;
                        sqlbulk.DestinationTableName = "Warn";

                        sqlbulk.ColumnMappings.Add(0, "HosNo");
                        sqlbulk.ColumnMappings.Add(1, "WarnNo");
                        sqlbulk.ColumnMappings.Add(2, "WarNature");
                        sqlbulk.ColumnMappings.Add(3, "WarType");
                        sqlbulk.ColumnMappings.Add(4, "WarBegindate");
                        sqlbulk.ColumnMappings.Add(5, "WarContent");
                        sqlbulk.ColumnMappings.Add(6, "WarDosage");
                        sqlbulk.ColumnMappings.Add(7, "WarWay");
                        sqlbulk.ColumnMappings.Add(8, "WarTime");
                        sqlbulk.ColumnMappings.Add(9, "WarExecutedate");
                        sqlbulk.ColumnMappings.Add(10, "WarFinishdate");
                        sqlbulk.ColumnMappings.Add(11, "WarnCondition");
                        sqlbulk.ColumnMappings.Add(12, "Warscalar");
                        sqlbulk.WriteToServer(dt);
                        System.Windows.Forms.MessageBox.Show("插入成功");


                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        dt.Dispose();
                    }
                }
                using (SqlBulkCopy sqlbc = new SqlBulkCopy(conn))
                {
                    try
                    {

                        sqlbc.BatchSize = dts.Rows.Count;
                        sqlbc.DestinationTableName = "ConsumptionSum";

                        sqlbc.ColumnMappings.Add(0, "HosNo");
                        sqlbc.ColumnMappings.Add(1, "WarNo");
                        sqlbc.ColumnMappings.Add(2, "WarContent");
                        sqlbc.ColumnMappings.Add(3, "MedDanwei");
                        sqlbc.ColumnMappings.Add(4, "Medprice");
                        sqlbc.ColumnMappings.Add(5, "MedModel");
                        sqlbc.ColumnMappings.Add(6, "MedAmount");
                        sqlbc.ColumnMappings.Add(7, "Medscalar");
                        sqlbc.ColumnMappings.Add(8, "AmountPayable");
                        sqlbc.ColumnMappings.Add(9, "AmountPaid");
                        sqlbc.ColumnMappings.Add(10, "RefundCondition");
                        sqlbc.WriteToServer(dts);
                        System.Windows.Forms.MessageBox.Show("插入成功");
                      

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    finally
                    {
                        dts.Dispose();
                    }
                }
              
               
            }
        }

        private void button_sc_Click(object sender, EventArgs e)
        {
             
            int n = dataGridView1.SelectedCells[0].RowIndex;
         
            double  p =Convert .ToDouble ( dts.Rows[n][6]);
            string HosBalance = "select HosBalance from InHospital where HosNo='" + tb_ZYNo.Text + "'";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                conn.Open();
                SqlCommand com = new SqlCommand(HosBalance, conn);
                double Balance = Convert.ToDouble(com.ExecuteScalar());
                float  NewBalance = (float)(Balance +p ) ;

                string abc = string.Format("update InHospital set HosBalance='" + NewBalance + "'where InHospital.HosNo='" + tb_ZYNo.Text .Trim () + "'");
                SqlCommand cam = new SqlCommand(abc, conn);
                int i = Convert.ToInt32(cam.ExecuteNonQuery());

                string HosTotal = "select HosTotal from InHospital where HosNo='" + tb_ZYNo.Text.Trim() + "'";
                SqlCommand cm = new SqlCommand(HosTotal, conn);
                double Total = Convert.ToDouble(cm.ExecuteScalar());
                float NewTotal = (float)(Total + p);
                string upt = string.Format("update InHospital set HosBalance='" + NewTotal + "'where InHospital.HosNo='" + tb_ZYNo.Text.Trim() + "'");
                SqlCommand cop = new SqlCommand(upt, conn);
                int j = Convert.ToInt32(cop.ExecuteNonQuery());


                if (i == 1&&j==1)
                {
                    MessageBox.Show("成组医嘱删除成功！");

                }
            }
            dataGridView1.Rows.RemoveAt(dataGridView1 .SelectedCells [0].RowIndex );
          dts.Rows.RemoveAt(n);
         
            
        }

      


    }    

    
}
